using System;
using System.Xml;

namespace LocalizationTest.Tools
{
  public static class ConfigurationTool
  {
    static private Configuration configuration;

    public static Configuration Configuration
    {
      get { return ConfigurationTool.configuration; }
    }
    static public void InitConfiguration(string filename)
    {
      configuration = new Configuration(filename);
    }
  }

	public class Configuration
	{
    private string _fileName;
    private XmlDocument _configurationXmlDocument;
    public XmlDocument XmlDocument
    {
      get{return _configurationXmlDocument;}
    }
    
    
    public Configuration(string fileName)
		{
      _fileName = fileName;

      if(!System.IO.File.Exists(fileName))
      {
        CreateConfigurationFile(fileName);
      }
      
      _configurationXmlDocument = new XmlDocument();
      try
      {
        _configurationXmlDocument.Load(fileName);
      }
      catch
      {
        if(System.IO.File.Exists(fileName + ".bak") )
          System.IO.File.Delete(fileName + ".bak");
        System.IO.File.Move(
          fileName,
          fileName + ".bak" );
        CreateConfigurationFile(fileName);
        _configurationXmlDocument.Load(fileName);
        System.Windows.Forms.MessageBox.Show(
          fileName + " was damaged - recreated it. \n" +
          "damaged version has been moved to" + 
          fileName + ".bak", 
          "RobotControl");
      }
		}

    public void WriteConfigurationEntry(string key, string valueString)
    {
      XmlNode node = GetXmlNode("configuration/" + key);
      node.InnerText = valueString;
      Save();
    }
    public void WriteConfigurationEntry(string key, bool valueBool)
    {
      XmlNode node = GetXmlNode("configuration/" + key);
      node.InnerText = valueBool.ToString();
      Save();
    }
    public string ReadConfigurationEntry(string key, string defaultString)
    {
      XmlNode node = _configurationXmlDocument.SelectSingleNode("configuration/" + key);
      if(node != null) return node.InnerText;
      else return defaultString;
    }

    public string ReadConfigurationEntry(string key)
    {
      return ReadConfigurationEntry("configuration/" + key, "");
    }

    public bool ReadConfigurationEntry(string key, bool defaultBool)
    {
      XmlNode node = _configurationXmlDocument.SelectSingleNode("configuration/" + key);
      if(node != null) return Boolean.Parse(node.InnerText);
      else return defaultBool;
    }

    public void Save()
    {
      lock (_configurationXmlDocument)
      {
        _configurationXmlDocument.Save(_fileName);
      }
    }


    private XmlNode GetXmlNode(string nodeString)
    {
      string[] parts = nodeString.Split('/');
      string currentString = "";
      XmlElement elementForInsertion = _configurationXmlDocument.DocumentElement;
      XmlNode currentNode;
      for(int i=0; i<parts.Length; i++)
      {
        currentString += "/" + parts[i];
        currentNode = null;
        currentNode = _configurationXmlDocument.SelectSingleNode(currentString);
        if(currentNode == null)
        {
          currentNode = _configurationXmlDocument.CreateElement(parts[i]);
          elementForInsertion.AppendChild(currentNode);
        }
        elementForInsertion = (XmlElement)currentNode;
      }
      return _configurationXmlDocument.SelectSingleNode(nodeString);
    }


    public static void CreateConfigurationFile(string fileName)
    {
      try
      {
        XmlTextWriter applicationConfiguration = 
          new XmlTextWriter(fileName, System.Text.Encoding.UTF8);

        applicationConfiguration.Formatting = Formatting.Indented;
				
        applicationConfiguration.WriteStartDocument();
        applicationConfiguration.WriteStartElement("configuration");
        applicationConfiguration.WriteEndElement();
        applicationConfiguration.WriteEndDocument();

        applicationConfiguration.Close();
      }
      catch (Exception exception)
      {
        System.Windows.Forms.MessageBox.Show("Exception: " + exception.ToString());
      }
    }

	}
}
